package com.sdjz.club.club.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdjz.club.club.entity.UserOrganization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdjz.club.club.entity.UserEvent;
import com.sdjz.club.club.service.impl.UserEventServiceImp;
import com.sdjz.club.club.mapper.UserEventMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dftre
 * @since 2022-07-15
 */
@RestController
@RequestMapping("/club/userEvent")
public class UserEventController {
    @Autowired
    UserEventServiceImp user_eventService;
    @Autowired
    UserEventMapper user_eventMapper;

    //查询数据总条数
    @RequestMapping("total")
    public CommonResult selectLength() {
        return new CommonResult(user_eventService.count());
    }

    //查询
    @GetMapping("/select")
    public CommonResult select(
            @RequestParam(name = "userId", required = false, defaultValue = "null") String user_id,
            @RequestParam(name = "eventId", required = false, defaultValue = "null") String event_id,
            @RequestParam(name = "role", required = false, defaultValue = "null") String role
    ) {
        QueryWrapper<UserEvent> wrapper = new QueryWrapper<>();
        if (!user_id.equals("null") && !event_id.equals("null")) {
            wrapper.eq("user_id", user_id);
            wrapper.eq("event_id", event_id);
            return CommonResult.succ(user_eventMapper.selectList(wrapper));
        } else if (!event_id.equals("null") && !role.equals("null")) {
            List<Object> ue = user_eventMapper.selectAllUserByEventId(event_id);
            List<Object> nue = new ArrayList<>();
            for (Object i : ue) {
                if (Objects.equals(String.valueOf(((Map<?, ?>) i).get("role")), role)) {
                    nue.add(i);
                }
            }
            return CommonResult.succ(nue);
        } else if (!user_id.equals("null") && !role.equals("null")) {
            List<Object> ue = user_eventMapper.selectAllEventByUserId(user_id);
            List<Object> nue = new ArrayList<>();
            for (Object i : ue) {
                if (Objects.equals(String.valueOf(((Map<?, ?>) i).get("role")), role)) {
                    nue.add(i);
                }
            }
            return CommonResult.succ(nue);
        } else if (!user_id.equals("null")) {
            return CommonResult.succ(
                    user_eventMapper.selectAllEventByUserId(user_id)
            );
        } else if (!event_id.equals("null")) {
            return CommonResult.succ(user_eventMapper.selectAllUserByEventId(event_id));
        } else if (!role.equals("null")) {
            wrapper.like("role", role);
            return CommonResult.succ(user_eventMapper.selectList(wrapper));
        } else {
            return CommonResult.succ();
        }
    }

    // 实现分页查询
    @GetMapping("list")
    public CommonResult selectPage(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size
    ) {
        Page<UserEvent> page = new Page<>(current, size);
        user_eventMapper.selectPage(page, null);
        return new CommonResult(page.getRecords());
    }

    // 实现删除
    @PostMapping("/delete")
    public CommonResult deleteMenu(@RequestBody UserEvent ue) {
        QueryWrapper<UserEvent> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ue.getUserId())
                .eq("event_id", ue.getEventId());
        return CommonResult.succ(user_eventMapper.delete(wrapper));
    }

    // 实现插入
    @PostMapping("/insert")
    public CommonResult addMenu(@RequestBody UserEvent ue) {
        return CommonResult.succ(user_eventMapper.insert(ue));
    }

    // 实现更新
    @PostMapping("/update")
    public CommonResult updateMenu(@RequestBody UserEvent ue) {
        QueryWrapper<UserEvent> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ue.getUserId())
                .eq("event_id", ue.getEventId());
        return CommonResult.succ(user_eventMapper.update(ue, wrapper));
    }
}

